/**
 * @file  数据中转站
 * @author  hj
 */
// 引入数据请求API
require('./login.css');
var nativeApi = require('nativeApi');
import {tools} from 'tools';
var lan = require('./language.json');
var ft = {};
ft.isDve = false; // true 静态数据 false动态数据
// console.info(document.domain);
// console.info('1111');

// var service = 'http://testobcpay.api.my7v.com/';
var service = tools.getHostFn();

// // 基金url
// ft.url = {
//   login: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/qrcode/qr',    // 登入
//   getUserInfo: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/qrcode/getUser',    // 获取个人信息
//   obtainCode: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/qrcode/sendAuthCode',    // 获取验证码
//   changePhone: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/qrcode/updatePhoneOrEmail',    // 修改电话或者邮箱
//   save: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/qrcode/qr',    // 个人信息保存
//   registerFn: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/qrcode/qr',    // 注册
//   ifExisted: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/qrcode/v1/ifExisted',    // 注册
//
//   // login: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service[0] + 'usercenter/user/qr',    // 登入
//   // getUserInfo: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service[0] + 'usercenter/user/qr_getUser',    // 获取个人信息
//   // obtainCode: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service[0] + 'usercenter/user/qr_sendAuthCode',    // 获取验证码
//   // changePhone: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service[0] + 'usercenter/user/qr_updatePhoneOrEmail',    // 修改电话或者邮箱
//   // save: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service[0] + 'usercenter/user/qr',    // 个人信息保存
//   // registerFn: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service[0] + 'usercenter/user/qr',    // 注册
//
//   paycenter: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/pay/v1/qr_sendAuthCode',    // 获取验证码
//   resetPassword: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/qrcode/resetPassword',    // 注册
//   getPayMent: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/pay/v1/getOrder',    // 获取订单详情
//   upload: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/qrcode/uploadAvatar',    // 附件上传
//   getPayInfo: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/user/v1/getUserForResetPayPwd',    // 获取个人信息
//   getReset: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/pay/v1/qrUpdatePassword',    // 重置支付密码
//   prepareEwalletPay: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/oceanpay/v1/prepareEwalletPay',    // 支付
//   DebitCard: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/oceanpay/v1/qrGetBankList',    // 账单
//   updatePayForm: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/oceanpay/v1/getOnlineBankPayOrder'    // 支付信息
// };
ft.url = {
  login: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/open/phone/qr',
  getUserInfo: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/open/phone/getUser',
  obtainCode: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/open/phone/sendAuthCode',
  changePhone: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/open/phone/updatePhoneOrEmail',
  save: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/open/phone/qr',
  registerFn: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/open/phone/qr',
  ifExisted: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/open/phone/v1/ifExisted',
  paycenter: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/open/phone/v1/qr_sendAuthCode',
  resetPassword: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/open/phone/resetPassword',
  getPayMent: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/open/phone/v1/getOrder',
  upload: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/open/phone/uploadAvatar',
  getPayInfo: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'usercenter/open/phone/v1/getUserForResetPayPwd',
  getReset: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/open/phone/v1/qrUpdatePassword',
  prepareEwalletPay: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/open/phone/v1/prepareEwalletPay',
  DebitCard: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/open/phone/v1/qrGetBankList',
  updatePayForm: ft.isDve ? 'http://localhost:63343/html5/json/login.json' : service + 'paycenter/open/phone/v1/getOnlineBankPayOrder'
};
export default {
  state: {
    loginData: {
      phone: '', // 18165785240
      password: '', // DAbao123
      entryType: 'LOGIN'
    },
    registerData: {
      username: '', // 18165785240
      authCode: '', // DAbao123
      password: '' // DAbao123,
    },
    editData: {
      url: '',
      birthday: '',
      nickName: '', // 18165785240
      authCode: '', // DAbao123
      password: '', // DAbao123,
      phone: '',
      email: ''
    },
    show: true,
    detailR: ''
  },
  language: lan[window.lan],
  /**
   * 登入接口
   * r 类型 QR
   * cb  请求成功之后回调函数
   */
  login: function (r, cb) {
    var self = this;
    tools.show(this.language.load);
    nativeApi.initAjax({
      'type': 'post',
      'url': ft.url.login,
      'param': {
        'params': {
          phone: self.state.loginData.phone.indexOf('@') > -1 ? null : self.state.loginData.phone,
          email: self.state.loginData.phone.indexOf('@') > -1 ? self.state.loginData.phone : null,
          password: self.state.loginData.password,
          entryType: 'LOGIN'
         },
        'r': r
      },
      callback: function (result) {
        tools.hide();
        if (result.data && result.data.errcode === '1000') {
          self.state.detailR = result.data.result;
          if (cb) {
            cb(result.data.result);
          }
        } else {
          var message = '';
          switch (result.data.errcode) {
            case '2015': message = self.language.QR; break;
            case '2002': message = self.language.UP; break;
            case '2000': message = self.language.AU; break;
            default: message = self.language.Fail;
          };
          tools.msgShow(message, 3000);
        }
      }
    });
  },
  /**
   * 注册登入接口
   * param 参数
   * r 类型 QR
   * type: 类型
   * cb  请求成功之后回调函数
   */
  registerFn: function (param, r, cb, type) {
    var self = this;
    if (param.password != param.passwords) {
      tools.msgShow(this.language.TPWA);
      return;
    };
    if (!(/^[0-9a-zA-Z]{6,100}$/.test(param.password))) {
      tools.msgShow(this.language.PMSC);
      return;
    };
    var entryType = '';
    var url = ft.url.registerFn;
    switch (type) {
      case 'forget': {
        entryType = 'RESET_PWD';
        break;
      }
      case 'register': {
        entryType = 'REGISTER';
        break;
      }
      case 'reset': {
        entryType = 'RESET_PWD';
        url = ft.url.resetPassword;
        break;
      }
    }
    tools.show(this.language.load);
    nativeApi.initAjax({
      'type': 'post',
      'url': url,
      'param': {
        'params': {
          'phone': param.phone.indexOf('@') > -1 ? null : param.phone,
          'email': param.phone.indexOf('@') > -1 ? param.phone : null,
          'password': param.password,
          'authCode': param.authCode,
          'entryType': entryType
        },
        'r': r
      },
      callback: function (result) {
        tools.hide();
        if (result.data && result.data.errcode == '1000') {
          if (type != 'reset') {
            self.state.detailR = result.data.result;
          };
          if (cb) {
            cb();
          }
        } else {
          var message = '';
          switch (result.data.errcode) {
            case '2015': message = self.language.QR; break;
            case '2001': message = self.language.alreadyRegistered; break;
            case '2011': message = self.language.codeNot; break;
            case '2012': message = self.language.PMSC; break;
            case '2000': message = self.language.AU; break;
            default: message = self.language.Fail;
          }
          tools.msgShow(message);
        }
      }
    });
  },
  /**
   * 编辑个人信息保存
   * param 参数
   * r 类型 QR
   * cb  请求成功之后回调函数
   */
  saveFn: function (param, r, cb) {
    var self = this;
    if (!param.nickName) {
      tools.msgShow(this.language.enterNickname);
      return;
    };
    if (param.nickName.length > 20) {
      tools.msgShow(this.language.NicknameV);
      return;
    };
    if (param.NewPass || param.oldPass || param.NewPassAg) {
      if (param.NewPass != param.NewPassAg) {
        tools.msgShow(this.language.TPWA);
        return;
      };
      if (!(/^[0-9a-zA-Z]{6,100}$/.test(param.NewPass))) {
        tools.msgShow(this.language.PMSC);
        return;
      };
    };
    tools.show(this.language.load);
    nativeApi.initAjax({
      'type': 'post',
      'url': ft.url.save,
      'param': {
        'params': {
          'updateData': {
            'nickName': param.nickName,
            'birthday': window.parseInt((param.birthday || '0').replace(/-/g, '')),
            'password': param.NewPassAg,
            'oldPassword': param.oldPass,
            'avatar': param.url
          }
        },
        'r': r
      },
      callback: function (result) {
        var message = '';
        tools.hide();
        if (result.data.errcode == '1000') {
          cb();
        } else {
          switch (result.data.errcode) {
            case '1000': message = self.language.Succeeded; break;
            case '2015': message = self.language.QR; break;
            case '2002': message = self.language.UP; break;
            default: message = self.language.Fail;
          };
          tools.msgShow(message, 4000);
        }
      }
    });
  },
  /**
   *  获取短信、邮箱验证码
   * param 手机或者邮箱账号
   * r 类型 QR
   * cb  请求成功之后回调函数
   */
  Obtain: function (param, r, cb, type) {
    var url = ft.url.obtainCode;
    var self = this;
    if (type == 'pay') {
      url = ft.url.paycenter;
    };
    nativeApi.initAjax({
      'type': 'post',
      'url': url,
      'param': {
        'params': {
           'phone': param.indexOf('@') > -1 ? null : param,
           'email': param.indexOf('@') > -1 ? param : null
        },
        'r': r
      },
      callback: function (result) {
        if (result.data && result.data.errcode != '1000') {
          var message = '';
          switch (result.data.errcode) {
            case '2015': message = self.language.QR; break;
            case '2000': message = self.language.correctAddress; break;
            case '2009': message = self.language.correctAddress; break;
            case '2010': message = self.language.correctAddress; break;
            case '1001': message = self.language.correctAddress; break;
            case '2011': message = self.language.correctAddress; break;
            default: message = self.language.Fail;
          };
          tools.msgShow(message, 3000);
          if (cb) {
            cb(result.data.errcode);
          }
        }
      }
    });
  },
  /**
   * 获取个人信息
   * r 类型 QR
   * cb  请求成功之后回调函数
   */
  getUserInfo: function (r, cb) {
    var self = this;
    // tools.show(this.language.load);
    nativeApi.initAjax({
      'type': 'get',
      'url': ft.url.getUserInfo,
      'param': {
        'r': r
      },
      callback: function (result) {
        // tools.hide();
        if (result.data && result.data.errcode == '1000') {
            self.state.editData = {
              url: result.data.result.avatar || '',
              phone: result.data.result.phone,
              nickName: result.data.result.nickName,
              birthday: tools.dateFn(String(result.data.result.birthday || 0)),
              email: result.data.result.email || ''
            };
            if (cb) {
              // self.state.editData = result.data.result;
              cb(result.data.result);
            }
        } else {
          var message = '';
          switch (result.data.errcode) {
            case '2015': message = self.language.QR; break;
            default: message = self.language.Fail;
          };
          tools.msgShow(message, 3000);
        }
      }
    });
  },
  /**
   * 获取支付的手机账户或者密码
   * r 类型 QR
   * cb  请求成功之后回调函数
   */
  getPayInfo: function (r, cb) {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': ft.url.getPayInfo,
      'param': {
        'requestCode': r
      },
      callback: function (result) {
        if (result.data && result.data.errcode == '1000') {
          if (cb) {
            cb(result.data.result);
          }
        } else {
          tools.msgShow(self.language.Fail, 3000);
        }
      }
    });
  },
  /**
   * 支付密码重置接口
   * param 参数
   * r 类型 QR
   * type: 类型
   * cb  请求成功之后回调函数
   */
  payResetFn: function (param, r, cb) {
    var self = this;
    if (param.password != param.passwords) {
      tools.msgShow(self.language.TPWA);
      return;
    };
    if (!(/^[0-9a-zA-Z]{6,100}$/.test(param.password))) {
      tools.msgShow(this.language.PMSC);
      return;
    };
    tools.show(this.language.load);
    nativeApi.initAjax({
      'type': 'post',
      'url': ft.url.getReset,
      'param': {
        'params': {
          'phone': param.phone.indexOf('@') > -1 ? null : param.phone,
          'email': param.phone.indexOf('@') > -1 ? param.phone : null,
          'password': param.password,
          'authCode': param.authCode
        },
        'r': r
      },
      callback: function (result) {
        tools.hide();
        if (result.data && result.data.errcode == '1000') {
          if (cb) {
            cb();
          }
        } else {
          var message = '';
          switch (result.data.errcode) {
            case '2015': message = self.language.QR; break;
            case '2000': message = self.language.notregistered; break;
            case '2011': message = self.language.codeNot; break;
            case '2012': message = self.language.PMSC; break;
            default: message = self.language.Fail;
          }
          tools.msgShow(message);
        }
      }
    });
  },
  /**
   * 获取支付订单状态-详情
   * orderNo 订单号
   * cb  请求成功之后回调函数
   */
  getPayMent: function (orderNo, cb) {
    // var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': ft.url.getPayMent,
      'param': {
        'orderNo': orderNo,
        'lang': window.lang
      },
      callback: function (result) {
        if (cb) {
          cb(result.data.result);
        }
        // if (result.data && result.data.errcode != '1000') {
        //   // tools.msgShow(self.language.Fail, 3000);
        // } else {
        //
        // }
      }
    });
  },
  /**
   * 编辑个人信息-更换手机号码
   * param 参数
   * r 类型 QR
   * cb  请求成功之后回调函数
   */
  changePhoneFn: function (param, r, cb, type) {
    var self = this;
    if (type == 'Email' && param.phone.indexOf('@') == -1) {
      tools.msgShow(self.language.mailboxFormat);
      return;
    };
    if (type == 'phone' && param.phone.indexOf('@') > -1) {
      tools.msgShow(this.language.phoneFormat);
      return;
    };
    nativeApi.initAjax({
      'type': 'post',
      'url': ft.url.changePhone,
      'param': {
        'params': {
          'phone': param.phone.indexOf('@') > -1 ? null : param.phone,
          'email': param.phone.indexOf('@') > -1 ? param.phone : null,
          'authCode': param.authCode
        },
        'r': r
      },
      callback: function (result) {
        if (result.data && result.data.errcode != '1000') {
          var message = '';
          switch (result.data.errcode) {
            case '2015': message = self.language.QR; break;
            case '2001': message = self.language.alreadyRegistered; break;
            case '2011': message = self.language.codeNot; break;
            case '2012': message = self.language.PMSC; break;
            default: message = self.language.Fail;
          }
          tools.msgShow(message, 3000);
        } else {
          tools.msgShow(self.language.changeSucceeded, 1000);
          if (cb) {
            cb(result.data.result);
          }
        }
      }
    });
  },

  /**
   * E-Wallet
   * param 参数
   * r 类型 QR
   * cb  请求成功之后回调函数
   */
  prepareEwalletPay: function (r, cb) {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': ft.url.prepareEwalletPay,
      'param': {
        'requestCode': r,
        'lang': window.lang
      },
      callback: function (result) {
        tools.hide();
        if (result.data && result.data.errcode != '1000') {
          var message = '';
          switch (result.data.errcode) {
            case '2015': message = self.language.QR; break;
            case '3011': message = self.language.unpayable; break;
            case '3009': message = self.language.Unableorder; break;
            default: self.language.Fail;
          };
          tools.msgShow(message, 4000);
        } else {
          if (cb) {
            cb(result.data.result);
          }
        }
      }
    });
  },
  /**
   * DebitCard 获取银行列表
   * r 类型 QR
   * cb  请求成功之后回调函数
   */
  DebitCard: function (r, cb) {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': ft.url.DebitCard,
      'param': {
        'r': r
      },
      callback: function (result) {
        tools.hide();
        if (result.data && result.data.errcode != '1000') {
          var message = '';
          switch (result.data.errcode) {
            case '1001': message = self.language.errororder; break;
            case '2015': message = self.language.QR; break;
            default: self.language.Fail;
          };
          tools.msgShow(message, 4000);
        } else {
          if (cb) {
            cb(result.data.result);
          }
        }
      }
    });
  },
  /**
   * updatePayForm 更新支付信息
   * r 类型 QR
   * cb  请求成功之后回调函数
   */
  updatePayForm: function (r, cb) {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': ft.url.updatePayForm,
      'param': {
        'requestCode': r,
        'lang': window.lang
      },
      callback: function (result) {
        tools.hide();
        if (result.data && result.data.result == '' || result.data.result == null) {
          tools.msgShow(self.language.errororder, 4000);
        } else {
          if (cb) {
            cb(result.data.result);
          }
        }
      }
    });
  },
  /**
   * 判断电话号码 是否存在
   * r 类型 QR
   * cb  请求成功之后回调函数
   */
  ifExisted: function (phone, r, cb, type) {
    var self = this;
    tools.show(this.language.load);
    nativeApi.initAjax({
      'type': 'get',
      'url': ft.url.ifExisted,
      'param': {
        phone: phone.indexOf('@') > -1 ? null : phone,
        email: phone.indexOf('@') > -1 ? phone : null,
        'r': r
      },
      callback: function (result) {
        tools.hide();
        var message = '';
        if (type == 'number') {
          if (result.data && result.data.errcode === '2000') {
            if (cb) {
              cb();
            }
          } else {
            switch (result.data.errcode) {
              case '2015': message = self.language.QR; break;
              case '2001': message = self.language.alreadyRegistered; break;
              default: message = self.language.Fail;
            };
            tools.msgShow(message, 3000);
          }
        } else if (type == 'register') {
          if (result.data && result.data.errcode === '2000') {
            if (cb) {
              cb();
            }
          } else {
            switch (result.data.errcode) {
              case '2015': message = self.language.QR; break;
              case '2001': message = self.language.alreadyRegistered; break;
              default: message = self.language.Fail;
            };
            tools.msgShow(message, 3000);
          }
        } else {
          if (result.data && result.data.errcode === '2001') {
            if (cb) {
              cb();
            }
          } else {
            switch (result.data.errcode) {
              case '2015': message = self.language.QR; break;
              case '2000': message = self.language.AU; break;
              default: message = self.language.Fail;
            };
            tools.msgShow(message, 3000);
          }
        }
      }
    });
  }
};
